

//组件工厂,即生成组件
require(['garenjs/utils','garenjs/htmlui','garenjs/easyui'],function(utils,garenUI,easyUI,aa){
	//验证jquery对象是否为空
	function checkJqObj(jqObj){
		if(jqObj && jqObj.length > 0) return true;
		return false;
	}
	$.fn.extend({/*扩展jquery dom操作库*/
		findOpts:function(name){
			var obj = name?this.find(name):this;
			var tagName = obj.prop('nodeName') || '';
			tagName = tagName.toLowerCase();
			return obj.data(tagName + "_ui");
		},
		findJq:function(name){
			var jq = this;
			if(name == undefined) return this;
			var rules = ['[uid="'+name+'"]',
			             '[uname="'+name+'"]',
			             '.' + name,
			             '#' + name,
			             '[nname="'+name+'"]',
			             '[name="'+name+'"]',
			             name];
			var jqObj = null;
			$.each(rules,function(i,rule){
				jqObj = jq.find(rule);
				if(checkJqObj(jqObj)) return false;
			});
			return jqObj;
		},
		findJqOpts:function(name){//多个集合只获取第一个
			var tagName = obj.prop('nodeName') || '';
			tagName = tagName.toLowerCase();
			return this.findJq(name).data(tagName + "_ui");
		},
		createUI:function(uiOpts){//渲染界面
			var jqObj = $(this);
			var newui = null,uis = [];
			if($.isArray(uiOpts) == false) uiOpts = [uiOpts];//默认为数组
			$.each(uiOpts,function(i,e){//遍历所有组件，自动匹配组件构造器
				if(e == null) return;
				if("string" == typeof e || 'number' == typeof e) jqObj.append(e);//文本直接添加
				else{
					var eName = e['eName'];
					if(typeof eName != "string") return true;
					newui = garenUI.createUI(jqObj,e);
					if(0 == newui){
						if(!e.uname) e.uname = e.eName;//复制ename
						if(!e.nname) e.nname = e.name;//复制name
						newui = easyUI.createUI(jqObj,e);
						if(!newui) $ui.print("组件不存在:" + eName);
					}else if(-1 == newui) $ui.print("组件验证没通过:" + eName);
					if('object' == typeof newui) uis.push(newui);
				}
			});
			if(uis.length == 1) return uis[0];
			else return uis;
		},
		loadJs:function(url,params,clearFlag){//远程加载
			if(typeof url != 'string') return;
			$ui.print(url);
			params = params || {};
			var ret = null;
			var jqObj = $(this);
			url = url.replace(/\.js$/i,'');
			if(clearFlag == null) jqObj.empty();
			require([url],function(mm){
				if($.isFunction(mm))
					ret =  new mm(jqObj,params);
			});
			return ret;
		},
		createHtml:function(uiOpts){
			$(this).append(garenUI.createHtmls(uiOpts));
		},
		//扩展组件方法,即JQuery EasyUI组件对象都可以调用
		updateOpt : function(uifun,newOpt){
		    try{
		    	if(this.length <= 0) return;
		        $.extend(this[uifun].call(this,'options'),newOpt);
		    }catch(err){
		        $ui.print(err);
		    }
		}
	});
});

